<?php
/**
 * Created by PhpStorm.
 * User: Lzhy ysjywz@gmail.com
 * Date: 2018/5/19
 * Time: 下午2:02
 */
namespace Core\common;

class Model
{
    public $table;
    private static $self;
    private $where = [];
    private $field = [];
    private $limit = [];
    private $order = '';
    private $keyField = '';

    /**
     * 表名
     * @param $table
     * @return $this
     */
    protected function table($table)
    {
        $this->table = $table;
        return $this;    
    }

    /**
     * 查询where条件
     * @param array $where
     * @return $this
     */
    public function where($where = [])
    {
        if($where){
            $this->where = array_merge($this->where,$where);
        }

        return $this;
    }

    /**
     * 排序
     * @param $order
     * @return $this
     */
    public function order($order)
    {
        $this->order = $order;
        return $this;
    }

    /**
     * 查询字段
     * @param $field
     * @return $this
     */
    public function field($field)
    {
        if(is_string($field)){
            $field = explode(',',trim($field,','));
        }
        $this->field = $field;

        return $this;
    }

    /**
     * 返回时数组键值
     * @param $key
     * @return $this
     */
    public function keyField($key)
    {
        if($key){
            $this->keyField = $key;
        }

        return $this;
    }

    /**
     * 微擎查询的limit可能有误(此处limit应该是页码才对)
     * @param $limit
     * @param string $end
     * @return $this
     */
    public function limit($limit,$end = '')
    {
        if($end){
            $this->limit = [$limit,$end];
            return $this;
        }
        $this->limit = $limit;

        return $this;
    }

    /**
     * 查询单条
     * @param $field
     * @return bool
     */
    public function find($field = '')
    {
        if($field){
            $this->field = $field;
            if(is_string($field)){
                $this->field = explode(',',$field);
            }
        }

        $data = pdo_get($this->table,$this->where,$this->field);
        $this->__endDestruct();
        return $data;
    }

    /**
     * 查询多条
     * @return array
     */
    public function select()
    {
        $data = pdo_getall($this->table,$this->where,$this->field,$this->keyField,$this->order,$this->limit);
        $this->__endDestruct();
        return $data;
    }

    /**
     * 更新
     * @param $data
     * @param array $where
     * @return bool
     */
    public function update($data,$where = [])
    {
        if($where) {
            $this->where = array_merge($this->where,$where);
        }

        $result = pdo_update($this->table,$data,$this->where);
        $this->__endDestruct();
        return $result;
    }

    /**
     * 删除
     * @param array $where
     * @return bool
     */
    public function delete($where = []){
        if($where) {
            $this->where = array_merge($this->where,$where);
        }

        $result = pdo_delete($this->table,$this->where);
        $this->__endDestruct();
        return $result;
    }

    /**
     * 添加
     * @param $data
     * @param string $table
     * @return bool
     */
    public function insert($data,$table = '')
    {
        if($table){
            $this->table = $table;
        }

        $result = pdo_insert($this->table,$data);
        $this->__endDestruct();
        return $result;
    }

    /**
     * 添加/更新
     * @param $data
     * @param array $where
     * @return bool
     */
    public function save($data,$where = [])
    {
        if(empty($where) && $data['id']){
            $where = ['id'=>$data['id']];
        }
        if($where || $this->where){
            $where = $where+$this->where;
            return $this->update($data,$where);
        }

        return $this->insert($data);
    }

    /**
     * 查询总条数
     * @param array $where
     * @return int
     */
    public function count($where = [])
    {
        if($where){
            $this->where = array_merge($this->where,$where);
        }
        $count = pdo_count($this->table,$this->where);
        $this->__endDestruct();

        return $count;
    }
    
    /**
     * 执行sql
     * @param $sql
     * @param array $params
     * @return bool
     */
    public function query($sql,$params = [])
    {
        return pdo_query($sql,$params);
    }

    public function __call($name, $arguments)
    {
        $instance = static::getInstance();
        if (method_exists($instance, $name)) {
            return call_user_func_array([$instance, $name], $arguments);
        }
        throw new \Exception('方法:'.$name.'不存在');
    }
    
    public static function __callStatic($name, $arguments)
    {
        $instance = static::getInstance();
        if (method_exists($instance, $name)) {
            return call_user_func_array([$instance, $name], $arguments);
        }
        throw new \Exception('方法:'.$name.'不存在');
    }
    
    private static function getInstance()
    {
        if (self::$self === null) {
            self::$self = new Model();
        }
        return self::$self;
    }

    private function __endDestruct()
    {
        $this->limit = [];
        $this->where = [];
        $this->field = [];
        $this->order = '';
        $this->keyField = '';
    }
}